﻿using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Protocols;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfigurationSection = System.Configuration.ConfigurationSection;

namespace Redis.Test
{
    public sealed class RedisConfig : ConfigurationSection
    {
        public static RedisConfig GetConfig()
        {
            RedisConfig section = GetConfig("RedisConfig");
            return section;
        }

        public static RedisConfig GetConfig(string sectionName)
        {
            RedisConfig section = (RedisConfig)ConfigurationManager.GetSection(sectionName);
            if (sectionName == null)
                throw new ConfigurationErrorsException("Section" + sectionName + "is not found.");
            return section;
        }

        /// <summary>
        /// 可读的Redis链接地址
        /// </summary>
        [ConfigurationProperty("WriteServerConstr", IsRequired = false)]
        public string WriteServerConstr
        {
            get
            {
                return (string)base["WriteServerConstr"];
            }
            set
            {
                base["WriteServerConstr"] = value;
            }
        }

        /// <summary>
        /// 最大读链接数
        /// </summary>
        [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
        public int MaxReadPoolSize
        {
            get
            {
                int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
                return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
            }
            set
            {
                base["MaxReadPoolSize"] = value;
            }
        }

        /// <summary>
        /// 本地缓存到期时间，单位:秒
        /// </summary>
        [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = 36000)]
        public bool AutoStart
        {
            get
            {
                return (bool)base["AutoStart"];
            }
            set
            {
                base["AutoStart"] = value;
            }
        }

        /// <summary>
        /// 是否记录日志，该设置仅用于排查Redis运行时出现的问题，入Redis工作正常，请关闭该项
        /// </summary>
        [ConfigurationProperty("RecordLog", IsRequired = false, DefaultValue = false)]
        public bool RecordLog
        {
            get
            {
                return (bool)base["RecordLog"];
            }
            set
            {
                base["RecordLog"] = value;
            }
        }
    }
}
